﻿using HandyControl.Controls;
using Mododor.DTO;
using Mododor.Ext;
using Mododor.JsonModels;
using Mododor.Models;
using Mododor.ViewInterfaces;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Documents;

namespace Mododor.ViewModels
{
    public class MainWindowViewModel : ViewInterface<MainWindowViewModel>
    {
        private string _title = "時間就是金錢";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        private readonly IRegionManager _regionManager;
        private MainWindowHeaderModel mainWindowHeader;

        public MainWindowHeaderModel MainWindowHeader
        {
            get { return mainWindowHeader; }
            set { mainWindowHeader = value; RaisePropertyChanged(); }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }
        private string accountInput;

        public string AccountInput
        {
            get { return accountInput; }
            set { accountInput = value; RaisePropertyChanged(); }
        }
        public ObservableCollection<NavigationModel> NavList { get; set; }
        public DelegateCommand<NavigationModel> SelectionChangedCommand { get; set; }
        public DelegateCommand ChangeAccountCommand { get; set; }
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand LoginCommand { get; set; }
        public MainWindowViewModel(IRegionManager region, IDialogService service, HttpHelper httpHelper) : base(service, httpHelper)
        {
            _regionManager = region;

            //将所有根界面添加入
            ObservableCollection<NavigationModel> navList = new ObservableCollection<NavigationModel>()
            {
                new NavigationModel(){ViewName="CompanyView",IconName="HandCoin",Name="客户", Role=1 },
                //new NavigationModel(){ViewName="CompanyView",IconName="HandCoin",Name="客户", Role=0 },
                //new NavigationModel(){ViewName="CompanyView",IconName="HandCoin",Name="客户", Role=0 },
                //new NavigationModel(){ViewName="CompanyView",IconName="HandCoin",Name="客户", Role=0 },
            };
            NavList = new ObservableCollection<NavigationModel>();

            SelectionChangedCommand = new DelegateCommand<NavigationModel>((x) =>
            {
                if (x == null)
                {
                    return;
                }
                _regionManager.Regions[Ext.ExtUrl.MainWindowRegionContent].RequestNavigate(x.ViewName);
            });

            ChangeAccountCommand = new DelegateCommand(() =>
            {
                Password = "";
                AccountInput = "";
                IsOpen = true;
            });

            CancelCommand = new DelegateCommand(() =>
            {
                IsOpen = false;
            });

            LoginCommand = new DelegateCommand(async () =>
            {
                _regionManager.Regions[Ext.ExtUrl.MainWindowRegionContent].RemoveAll();
                NavList.Clear();
                UserInfoRequestDTO userDTO = new UserInfoRequestDTO()
                {
                    Password = Password,
                    UserName = AccountInput
                };
                string json = JsonConvert.SerializeObject(userDTO);
                string jsonRes = await _httpHelper.PostJson(ExtUrl.UserAuthUrl, json);
                UserAuthJsonModel.Rootobject root = JsonConvert.DeserializeObject<UserAuthJsonModel.Rootobject>(jsonRes);
                if (root.status == 200)
                {
                    //分配权限
                    int role = int.Parse(root.res.res);
                    string username = "张三";
                    if (role == 1)
                    {
                        MainWindowHeader = new MainWindowHeaderModel() { AccIconName = "AccountTieHat", AccName = username, AccRole = "超级管理员" };
                        NavList.AddRange(navList.Where(x => x.Role == 1));
                    }
                    else
                    {
                        MainWindowHeader = new MainWindowHeaderModel() { AccIconName = "Account", AccName = username, AccRole = "普通账号" };
                        NavList.AddRange(navList.Where(x => x.Role == 0));
                    }
                    IsOpen = false;

                }
            });
        }
    }
}
